import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/mine_business/ai/ai_record_logic.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

class AiRecordPage extends StatefulWidget {
  final int position; // 0 脱衣, 1 视频 2， 图片

  const AiRecordPage({super.key, this.position = 0});

  @override
  State<StatefulWidget> createState() {
    return _AiRecordPageState();
  }
}

class _AiRecordPageState extends State<AiRecordPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AIRecordBaseLogic>(
      init: instanceController(widget.position),
      global: false,
      builder: (controller) {
        return Column(
          children: [
            TabBar(
              controller: controller.tabController,
              tabs: List.generate(
                controller.subTypes.length,
                (index) => Container(
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  child: Text(controller.subTypes[index]),
                  decoration: BoxDecoration(
                    border: index != 2
                        ? Border(
                            right: BorderSide(width: .5, color: Colors.white.withOpacity(.1)),
                          )
                        : null,
                  ),
                ),
              ),
              onTap: (index) {
                controller.sort = index + 1;
                controller.fetchPageData(needLoading: true);
              },
              isScrollable: true,
              padding: EdgeInsets.only(top: 10, left: 7),
              labelPadding: EdgeInsets.zero,
              tabAlignment: TabAlignment.start,
              unselectedLabelStyle: TextStyle(color: Colors.white.withOpacity(.35), fontSize: 14),
              labelStyle: TextStyle(color: Colors.white.withOpacity(.9), fontSize: 14),
              indicator: BoxDecoration(),
            ),
            Expanded(
              child: pullYsRefresh(
                onLoading: (_) => controller.fetchPageData(isRefresh: false),
                onRefresh: (_) => controller.fetchPageData(),
                child: () {
                  if (controller.isLoading) return LoadingCenterWidget();
                  if (controller.dataSource.isEmpty) return CErrorWidget();
                  return ListView.separated(
                    padding: EdgeInsets.only(left: 16, right: 16, top: 18),
                    separatorBuilder: (_, index) => 12.sizeBoxH,
                    itemCount: controller.dataSource.length,
                    itemBuilder: (BuildContext context, int index) {
                      return controller.instanceChild(index);
                    },
                  );
                }(),
                onInit: (ctr) => controller.refreshController = ctr,
              ),
            )
          ],
        );
      },
    );
  }
}
